let log = require('../../util/log.js');

module.exports = Utils;
let seqLen = [0, 5, 3, 2];
function Utils(size) {
    this._size = size;  //1,2,3: 单顺,双顺,三顺
}

// 合并数组
let _concat = function (vectors) {
    let arr = [];
    return arr.concat.apply(arr, vectors);
};

//返回:{shun:顺,dan:单牌,check:要重新检测的牌}
let resolve = function (cardList, seqLen) {
    // cardList.sort();     // 请确保已经在外面排过序了
    let len = cardList.length;
    let x = cardList[0];    // 保存上一个值
    let temp = [];          // 临时存放可能是连续的牌
    let rs = [];            // 保存所有的顺子到这里
    let cs = [];            // 临时存放可能需要再次检查的牌,有可会挪到单牌表中
    let checks = [];        // 保存确认要再次检查的牌
    let single = [];        // 保存所有的单牌,最后合并成一个大数组
    let except = [];        // 大于A的排, 不算顺子

    let push = function () {
        if (temp.length > 0) {
            if (temp.length >= seqLen) {        // 长度大于等于5才算连子
                rs.push(temp);
                checks.push(cs);
            }
            else {                              // 否则只能算是单牌
                single.push(temp);
                single.push(cs);
            }
        }
    }
    if (x != null)
        temp.push(x);
    for (let i = 1; i < len; i++) {
        let c = cardList[i];
        if (c.getValue() >= 12) {
            except.push(c);
            continue;
        }
        if (x.getValue() + 1 != c.getValue()) {
            if (x.getValue() == c.getValue()) {
                cs.push(c);
                continue;
            }
            push();
            temp = [];
            cs = [];
        }

        temp.push(c);
        x = c;
    }
    push();
    single.push(except);

    // console.log('except:',except,rs)
    return {'shun': rs, 'dan': _concat(single), 'check': _concat(checks)};
}

// 将cardList拆成多个连子, 达到单牌最少化
let recombine = function (cardList, checkList, seqSize) {
    let rs = [];
    let dan = [];
    let checkLen = cardList.length - 5;
    for (let j = 0,i = 0; j < checkList.length; j++) {
        let x = checkList[j];
        if (x.getValue() < cardList[i].getValue()) {
            dan.push(x);
            continue;
        }

        for (; i <= checkLen; i++) {
            if (cardList[i].getValue() == x.getValue()) break;          // 查找x在cardList中的位置,
        }
        if (i > checkLen) {
            break;                          // 没有相同的值,退出循环了.
        }
        let tail = checkList.slice(j);
        let v = cardList.slice(0, i);             // 截取一个vector
        let d = resolve(v.concat(tail), seqLen[seqSize])

        if (d.shun.length == 0) {
            dan.push(x);
            continue;// 检查失败了
        }
        rs.push(d.shun[0]);
        checkList = d.dan.concat(d.check);
        if (i < checkLen && checkList.length > 0) {
            // 还有新的检查要做, 取顺子的剩下的部分,和新的check数据一起再查一次
            let r = recombine(cardList.slice(i), checkList, seqSize);
            let _shun = r[0];
            let _dan = r[1];
            for (let a in _shun) {
                rs.push(_shun[a]);
            }
            dan = dan.concat(_dan);
            return [rs, dan];
        }
        else {
            rs.push(cardList.slice(i));
            dan = dan.concat(d.dan);
            return [rs, dan];
        }
    }
    return [[cardList], checkList];
}
Utils.sort = function (cardList) {
    cardList.sort(function (a, b) {
        return a.getValue() - b.getValue();
    })
}

// 从cardList中提取顺子, len为顺的长度
Utils.getSequence = function (cardList, size) {
    Utils.sort(cardList);
    //分成多个顺子和单牌
    if (size == undefined) size = 1;
    let ll = resolve(cardList, seqLen[size]);
    let rs = [];
    let checkList = ll.check;  // 要再次检查的散牌
    cardList = ll.shun;   // 顺子
    for (let i in cardList) {
        // 将散牌和顺子重新重合,尽量减少散牌
        let xx = recombine(cardList[i], checkList, size);
        // 更新散牌
        checkList = xx[1];
        // 保存拆分后的顺子        
        for (let j = 0; j < xx[0].length; j++) {
            rs.push(xx[0][j]);
        }
    }
    // console.log("最终效果:", rs);
    return {seq: rs, dan: ll.dan, check: checkList} //rs.push(checkList.concat(ll.dan));
}

// 统计牌型数据
// 返回[单牌,对子,三张,炸弹]
Utils.classify = function (cardList) {
    Utils.sort(cardList);
    let diff = [[], [], [], []];
    let count = [0, 0, 0, 0];
    let sum = 0;            // 不同牌的数量
    let a = cardList[0];    // 上一个值
    let temp = [];
    
    let len = cardList.length;
    if (len >= 2) {
        let w = cardList[len - 2];
        let W = cardList[len - 1];
        if (w.getValue() == 13 && W.getValue() == 14) {
            diff[4] = [w, W];
            len -= 2;
        }
    }

    for (let i = 0; i < len; i++) {
        let v = cardList[i]; //.getValue();
        if (a.getValue() != v.getValue()) {
            let n = temp.length - 1;
            for (let j = 0; j < temp.length; j++) {
                try {
                    diff[n].push(temp[j]);
                } catch (e){
                    log.e('Utils.classify',n,temp);
                    log.e(e);
                }
            }
            count[n] = count[n] + 1;
            temp = [];
            sum++;
        }
        a = v;
        temp.push(v);
        console.log(temp)
    }

    if (temp.length > 0) {
        let n = temp.length - 1;
        for (let j = 0; j < temp.length; j++)
            diff[n].push(temp[j]);
        count[n] = count[n] + 1;
        sum++;
    }
    //返回统计信息
    return {'group': diff, 'count': count, 'sum': sum};
}